package com.kaifamiao.wendao.service;

import com.kaifamiao.wendao.dao.CustomerDao;
import com.kaifamiao.wendao.dao.ExplainDao;
import com.kaifamiao.wendao.dao.TopicDao;
import com.kaifamiao.wendao.entity.Customer;
import com.kaifamiao.wendao.entity.Explain;
import com.kaifamiao.wendao.entity.Topic;
import com.kaifamiao.wendao.utils.Pagination;
import com.kaifamiao.wendao.utils.SnowflakeIdGenerator;

import java.time.LocalDateTime;
import java.util.List;

public class TopicService {

    private CustomerDao customerDao = new CustomerDao();
    private TopicDao topicDao = new TopicDao();
    private ExplainDao explainDao = new ExplainDao();
    // 获取标识符生成器(该对象在当前JVM中是单例)
    private SnowflakeIdGenerator idGenerator = SnowflakeIdGenerator.getInstance();

    public boolean save(Topic t) {
        t.setId(idGenerator.generate());
        t.setPublishTime(LocalDateTime.now());
        return topicDao.save(t);
    }

    public List<Topic> loadAll(){
        List<Topic> topics = topicDao.findAll();
        for(Topic t : topics){
            // 从Topic实例中获得Customer
            Customer c = t.getAuthor();
            // 根据Customer的id重新查询其详细信息
            c = customerDao.find(c.getId());
            // 为Topic实例重新设置发布者信息
            t.setAuthor(c);
        }
        return topics;
    }

    public Topic load(final Long id) {
        // 根据 topic id 获取单个Topic实例
        Topic t = topicDao.find(id);

        // 从Topic实例中获得Customer实例(其中只有一个 customer id 值)
        Customer c = t.getAuthor();
        // 根据 customer id 查询用户信息
        c = customerDao.find(c.getId());
        // 将查询后得到的Customer实例重新设置到Topic实例的author属性
        t.setAuthor(c);

        // 根据 topic id 查询所有的"解答"(explain)
        List<Explain> explains = explainDao.findByTopicId(id);
        for(Explain e : explains){
            Customer a = e.getAuthor();
            a = customerDao.find(a.getId());
            e.setAuthor(a);
        }
        // 为当前话题设置"解答"列表
        t.setExplains(explains);

        return t;
    }

    public Pagination<Topic> loadByCustomer(Customer me, int current, int size) {
        Long cid = me.getId();
        // 统计某个用户所发表的话题数目
        final int rowCount = topicDao.count(cid);
        int total = rowCount / size ; // 整除求商
        total = rowCount % size != 0 ? total + 1 : total;
        // 根据用户编号查询指定用户发布的所有话题
        List<Topic> list = topicDao.findByCustomerId(cid, current, size);
        for(Topic t : list){
            t.setAuthor(me);
        }
        return new Pagination<Topic>(size,total,current,list);
    }

    /**
     * 根据分页条件查询数据库并返回Pagination实例
     * @param current 当前页码(即需要查询的那一页)
     * @param size 每页至多显示的行数
     * @return 返回封装了分页条件和分页数据的Pagination实例
     */
    public Pagination<Topic> load(int current, int size) {
        // 统计话题表中的记录数目
        final int rowCount = topicDao.count();
        int total = rowCount / size ; // 整除求商
        total = rowCount % size != 0 ? total + 1 : total;
        // 根据分页条件查询数据
        List<Topic> list = topicDao.find(current, size);
        for(Topic t : list){
            // 从Topic实例中获得Customer
            Customer c = t.getAuthor();
            // 根据Customer的id重新查询其详细信息
            c = customerDao.find(c.getId());
            // 为Topic实例重新设置发布者信息
            t.setAuthor(c);
        }
        Pagination<Topic> p = new Pagination<>(size,total,current,list);
        return p;
    }
}
